﻿using ASmile.DBModel;
using System;
using System.Windows.Forms;

namespace ASmile.Win.ADSearch
{
    public partial class FrmADRecord : FrmBaseModal
    {
        public FrmADRecord()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 选中的行数据
        /// </summary>
        public Sys_ADRecord SelectedRecord { get; private set; }
        /// <summary>
        /// 查询记录的方法
        /// </summary>
        public Func<string, object> GetDataSource;
        /// <summary>
        /// 保存记录的方法
        /// </summary>
        public Func<Sys_ADRecord, ApiResult<string>> SaveRecord;
        /// <summary>
        /// 删除记录的方法
        /// </summary>
        public Func<Sys_ADRecord, ApiResult> DelRecord;

        void BindDataSource()
        {
            dgv.DataSource = GetDataSource(tsTxtSearch.Text);
        }

        private void FrmADRecord_Load(object sender, EventArgs e)
        {
            BindDataSource();
            tsTxtSearch.Focus();
            tsTxtSearch.SelectAll();
        }

        private void tsBtnSelected_Click(object sender, EventArgs e)
        {
            SelectedRecord = dgv.GetSelectedClassData<Sys_ADRecord>();
            if (SelectedRecord == null)
            {
                ShowMessage("未选中任何记录");
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var sel = dgv.GetSelectedClassData<Sys_ADRecord>();
            if (sel == null)
            {
                ShowMessage("未选中任何记录");
                return;
            }

            FrmADSave frm = new FrmADSave();
            var arg = new EditFormArgs
            {
                SaveHandle = (m) => SaveRecord(m as Sys_ADRecord),
                SaveModel = sel,
                SetModel = sel
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                ShowMessage(ret.msg);
                dgv.Refresh();
            }
        }

        private void tsbtnDelete_Click(object sender, EventArgs e)
        {
            var sel = dgv.GetSelectedClassData<Sys_ADRecord>();
            if (sel == null)
            {
                ShowMessage("未选中任何记录");
                return;
            }
            if (!ShowQuestion("确认要删除吗?", "删除确认"))
            {
                return;
            }
            var r = DelRecord(sel);
            if (r)
            {
                dgv.DelDataSource(sel);
            }
            ShowMessage(r.msg);
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindDataSource();
            }
            if (e.KeyCode == Keys.Down)
            {
                dgv.Focus();
            }
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tsBtnSelected_Click(sender, e);
            }
        }

        private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            tsBtnSelected_Click(sender, e);
        }
    }
}
